<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 11:18
 */

namespace Modules\Admin\Service;

use HiCommon\Repository\ManualRepository;
use HiCommon\Repository\UpKeepTypeRepository;
use HiCommon\Repository\AppDownloadRepository;

class ManualService
{
    /**
     *  获取原厂配件参数数据
     *  hinq 2019 11 09
     *
     **/
    public function get_list_original(array $arr_search)
    {
        try {
            $arr_option['where'] = ['type' => 2];
            $arr_option['order'] = ['key' => 'status', 'value' => 'desc'];
            $arr_list = ManualRepository::get_list((array)$arr_option);
            $arr_list->appends($arr_search);//添加链接参数
            $arr_list = list_page_data($arr_list, $arr_search);
            return hello_success('成功', $arr_list);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     *  获取指定的数据
     *  hinq 2019 11 09
     *
     **/
    public function get_one(int $nId)
    {
        try {
            if (empty($nId)) {
                return hello_error('参数错误');
            }
            $arr_info = ManualRepository::get_one((int)$nId)->toArray();
            return $arr_info;
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     *  编辑指定的数据
     *  hinq 2019 11 09
     *
     **/
    public function edit_one(array $arr_input)
    {
        try {
            if (empty($arr_input['content'])) {
                return hello_error('内容不能为空');
            }
            $data['id'] = $arr_input['id'];
            $data['name'] = $arr_input['name'];;
            $data['content'] = $arr_input['content'];
            if (empty($arr_input['is_default'])) {
                $data['is_default'] = 0;
            } else {
                $data['is_default'] = 1;
            }
            if ($data['is_default'] == 1) {    //如果选择了默认
                //将之前的数据修改为已编辑
                $where['type'] = 2;
                $where['status'] = 1;
                $arr_data['is_default'] = 2;
                ManualRepository::edit_one_where((array)$where, (array)$arr_data);
            }
            $res = ManualRepository::edit_one((array)$data);
            if (empty($res)) {
                return hello_error('修改失败');
            }
            return hello_success('修改成功');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }


    /**
     *  删除原厂配件参数
     *  hinq 2019 11 09
     *
     **/
    public function delete_one(int $nId)
    {
        try {
            if (empty($nId)) {
                return hello_error('参数错误');
            }
            $res = ManualRepository::delete_one((int)$nId);
            if (empty($res)) {
                return hello_error('删除失败');
            }
            return hello_success('删除成功');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     *  获取保养项目数据
     *  hinq 2019 11 15
     *
     **/
    public function get_list_upkeep(array $arr_search)
    {
        try {
            $arr_option['where'] = ['status' => 1];
            $arr_option['order'] = ['key' => 'sort', 'value' => 'asc'];
            $arr_list = UpKeepTypeRepository::get_list((array)$arr_option);
            $arr_list->appends($arr_search);//添加链接参数
            $arr_list = list_page_data($arr_list, $arr_search);
            return hello_success('ok', $arr_list);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     *  添加保养项目数据
     *  hinq 2019 11 15
     *
     **/
    public function store_upkeep(array $arr_input)
    {
        try {
            if (empty($arr_input['title'])) {
                return hello_error('保养项目名称不能为空');
            }
            if (empty($arr_input['remark'])) {
                return hello_error('保养项目说明不能为空');
            }
            $data['title'] = $arr_input['title'];
            $data['status'] = 1;
            $data['remark'] = $arr_input['remark'];
            if (!empty($data['sort'])) {
                $data['sort'] = 200;
            } else {
                $data['sort'] = $arr_input['sort'];
            }
            $res = UpKeepTypeRepository::add_one((array)$data);
            if (empty($res)) {
                return hello_error('添加失败');
            }
            return hello_success('添加成功');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     *  编辑保养项目视图
     *  hinq 2019 11 15
     *
     **/
    public function get_one_upkeep(int $nId)
    {
        try {
            if (empty($nId)) {
                return hello_error('参数错误');
            }
            $arr_info = UpKeepTypeRepository::get_one((int)$nId)->toArray();
            return hello_success('ok', $arr_info);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     *  编辑保养项目数据
     *  hinq 2019 11 15
     *
     **/
    public function update_upkeep(array $arr_input)
    {
        try {
            if (empty($arr_input['title'])) {
                return hello_error('保养项目名称不能为空');
            }
            if (empty($arr_input['remark'])) {
                return hello_error('保养项目说明不能为空');
            }
            $data['title'] = $arr_input['title'];
            $data['remark'] = $arr_input['remark'];
            $data['id'] = $arr_input['id'];
            if (!empty($data['sort'])) {
                $data['sort'] = 200;
            } else {
                $data['sort'] = $arr_input['sort'];
            }
            $res = UpKeepTypeRepository::edit_one((array)$data);
            if (empty($res)) {
                return hello_error('编辑失败');
            }
            return hello_success('编辑成功');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     *  删除指定的保养项目类型数据
     *  hinq 2019 11 16
     *
     **/
    public function delete_upkeep(int $nId)
    {
        try {
            if (empty($nId)) {
                return hello_error('参数错误');
            }
            $res = UpKeepTypeRepository::delete_one((int)$nId);
            if (empty($res)) {
                return hello_error('删除失败');
            }
            return hello_success('删除成功');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }

    }


}